﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class Form1 : Form
    {
        User user;

        internal User User
        {
            get { return user; }
            set { user = value; }
        }
        public Form1()
        {
            InitializeComponent();
        }

        internal Form1(User user)
        {
            InitializeComponent();
            this.user = user;
            txt_id.Text = user.Userid;
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            string userid = txt_id.Text;
            string pwd = txt_pwd.Text;
            string sql = "select * from dbo.users where userid=@userid and pwd=@pwd";
            SqlParameter[] arraysql = { new SqlParameter("@userid",userid ), new SqlParameter("@pwd", pwd) };
            SqlDataReader sdr = DBAccess.GetReader(sql, arraysql);
            if (sdr.Read() == true)
            {
                if (sdr["auth"].ToString()=="3")
                {
                    User u = new User(sdr["userid"].ToString(), sdr["username"].ToString());
                    StudentLogin sl = new StudentLogin();
                    sl.Welcome(u);
                    sl.User = u;
                    sl.Show();
                }
                else if (sdr["auth"].ToString() == "2")
                {
                    User u = new User(sdr["userid"].ToString(), sdr["username"].ToString());
                    TeacherLogin tl = new TeacherLogin();
                    tl.Welcome(u);
                    tl.User = u;
                    tl.Show();
                }else{
                    User u = new User(sdr["userid"].ToString(), sdr["username"].ToString());
                    RootLogin tl = new RootLogin();
                    tl.Welcome(u);
                    tl.User = u;
                    tl.Show();
                }
               this.Hide();
            }
            else
            {
                MessageBox.Show("账号或密码错误");
            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void txt_id_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}
